<?php

namespace app\admin\model;

use app\common\library\Auth;
use app\common\model\Token;
use think\Db;
use think\Model;

class User extends Model
{
	
	// 表名
	protected $name = 'user';
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	// 追加属性
	protected $append = [
		'prevtime_text',
		'logintime_text',
		'jointime_text',
		'level_text',
		'gender_text',
		'status_text',
	];
	
	protected static function init()
	{
		self::beforeUpdate(function ($row) {
			$changed = $row->getChangedData();
			//如果有修改密码
			if (isset($changed['password']))
			{
				$salt = \fast\Random::alnum();
				$row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
				$row->salt = $salt;
			}
		});
	}
	
	
	const LEVEL_0 = 0;
	const LEVEL_1 = 1;
	const LEVEL_2 = 2;
	const LEVEL_3 = 3;
	const LEVEL_4 = 4;
	
	public static $_level = [
		self::LEVEL_0 => '普通用户',
		self::LEVEL_1 => '初级VIP',
		self::LEVEL_2 => '店主',
		self::LEVEL_3 => '社区合伙人',
		self::LEVEL_4 => '区域运营商',
	];
	
	public function getLevelList(){
		return self::$_level;
	}
	
	public function getLevelTextAttr($value, $data) {
		$value = $value ? $value : $data['level'];
		return isset(self::$_level[$value]) ? self::$_level[$value] : '';
	}
	
	const GENDER_MALE = 1;
	const GENDER_FEMALE = 0;
	
	public function getGenderList()
	{
		return [self::GENDER_MALE => '男', self::GENDER_FEMALE => '女'];
	}
	
	public function getGenderTextAttr($value, $data) {
		$value = $value ? $value : $data['gender'];
		$list = $this->getGenderList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	const STATUS_Y = 1;
	const STATUS_N = 0;
	
	public function getStatusList()
	{
		return [self::STATUS_Y => '正常', self::STATUS_N => '禁用'];
	}
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = $this->getStatusList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	
	public function getPrevtimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['prevtime'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getLogintimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['logintime'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getJointimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['jointime'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	protected function setPrevtimeAttr($value)
	{
		return $value && !is_numeric($value) ? strtotime($value) : $value;
	}
	
	protected function setLogintimeAttr($value)
	{
		return $value && !is_numeric($value) ? strtotime($value) : $value;
	}
	
	protected function setJointimeAttr($value)
	{
		return $value && !is_numeric($value) ? strtotime($value) : $value;
	}
	
	public function group()
	{
		return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	public function recommend(){
		return $this->belongsTo('User', 'recommend_id', 'id', '', 'LEFT')->setEagerlyType(0);
	}
	
	
	/**
	 * 查看用户的信息，推荐人，实名认证，token
	 * @param $id
	 * @return array|false|\PDOStatement|string|Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function getRelationInfo($id){
		$userInfo = Db::name('user')->alias('u')
			->join('userVerified v', 'u.id = v.user_id', 'left')
			->join('user r', 'r.id = u.recommend_id', 'left')
			->field('u.id, u.username, u.nickname, u.avatar as head_ico, u.gender as sex, u.email, u.birthday, null as weixin, u.pay_password, u.mobile, u.level, u.integral, from_unixtime(u.jointime) as join_time, u.status, u.agent, u.balance, u.phone_balance, u.voucher_balance, u.phone_time, v.true_name, v.card_no, v.status as card_status, v.remark as verified_remark, r.mobile as recommend_mobile, r.username as recommend_username, u.password, u.pay_password, u.deposit_balance, u.video_time')
			->find($id);
		
		$userInfo['level_text'] = self::$_level[$userInfo['level']];
		$userInfo['card_status'] = $userInfo['card_status']?:0;
		$userInfo['video_valid'] = $userInfo['video_time'] > time() ? 1 :0;
		$userInfo['video_time'] = $userInfo['video_time']? date("Y-m-d",$userInfo['video_time']):'';
		$userInfo['phone_time'] = $userInfo['phone_time']? date("Y-m-d",$userInfo['phone_time']):'';
		$auth = Auth::instance();
		$userInfo['token'] = \app\common\library\Token::get($auth->getToken())['token'];
		return $userInfo;
	}
}
